package org.black.missedCall2.reply.contacts.dataMOdel;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.black.missedCall2.R;

import android.content.ContentUris;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactListAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater layoutInflater;
	private List<ContactDataModel> data;

	public ContactListAdapter(Context context, List<ContactDataModel> data) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.data = data;
	}

	@Override
	public int getCount() {
		return this.data.size();
	}

	@Override
	public Object getItem(int position) {
		return this.data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = this.layoutInflater.inflate(
					R.layout.contact_list_row, null);
			ImageView contactPhoto = (ImageView) convertView
					.findViewById(R.id.contactListRowContactPhoto);
			viewHolder.contactPhoto = contactPhoto;
			TextView nameTextView = (TextView) convertView
					.findViewById(R.id.contactListRowNameTextView);
			viewHolder.nameTextView = nameTextView;
			TextView phoneTextView = (TextView) convertView
					.findViewById(R.id.contactListRowPhoneTextView);
			viewHolder.phoneTextView = phoneTextView;

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ContactDataModel contactDataModel = (ContactDataModel) this
				.getItem(position);
		viewHolder.nameTextView.setText(contactDataModel.getName());
		if (TextUtils.isEmpty(contactDataModel.getPhoneNumber()) == false) {
			viewHolder.phoneTextView.setText(contactDataModel.getPhoneNumber());

		}
		if (contactDataModel.getContactId() != null) {
			Uri uri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI,
					contactDataModel.getContactId());
			InputStream input = ContactsContract.Contacts
					.openContactPhotoInputStream(
							this.context.getContentResolver(), uri);
			if (input != null) {
				viewHolder.contactPhoto.setImageBitmap(BitmapFactory
						.decodeStream(input));
			}
			try {
				if (input != null) {
					input.close();
				}
			} catch (IOException e) {
				viewHolder.contactPhoto.setImageBitmap(BitmapFactory
						.decodeResource(this.context.getResources(),
								R.drawable.ic_contact_picture));
			}
		} else {
			viewHolder.contactPhoto.setImageBitmap(BitmapFactory
					.decodeResource(this.context.getResources(),
							R.drawable.ic_contact_picture));
		}
		return convertView;
	}

	private static class ViewHolder {
		ImageView contactPhoto;
		TextView nameTextView;
		TextView phoneTextView;
	}

}
